<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学员名称" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入支付学员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="providerId">
        <el-input
          v-model="queryParams.providerId"
          placeholder="请输入供应商名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>   
      <el-form-item label="科目" prop="subjectId">
        <el-input
          v-model="queryParams.subjectId"
          placeholder="请输入科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="subjectDeptId">
        <el-input
          v-model="queryParams.subjectDeptId"
          placeholder="请输入专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item label="订单状态" prop="orderStatus">
        <el-input
          v-model="queryParams.orderStatus"
          placeholder="请输入订单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange"> 
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="学员名称" align="center" prop="userId" /> 
      <el-table-column label="学员电话" align="center" prop="phone" /> 
      <el-table-column label="专业" align="center" prop="subjectDeptId" /> 
      <el-table-column label="课程" align="center" prop="subjectId" />
      <!-- <el-table-column label="状态1待支付，2学习中，3已取消，4已取消" align="center" prop="orderStatus" /> -->
      <el-table-column label="订单状态" align="center" prop="orderStatus" /> 
      <el-table-column label="应付金额" align="center" prop="actualPrice" />
      <el-table-column label="实付金额" align="center" prop="payPrice" />
      <el-table-column label="支付时间" align="center" prop="paytime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paytime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付流水号" align="center" prop="payNo" />
      <el-table-column label="订单备注" align="center" prop="remark" />
      <!-- <el-table-column label="支付渠道：ALI支付宝  WX微信  EX兑换码兑换" align="center" prop="payChannel" /> -->
      <el-table-column label="支付渠道" align="center" prop="payChannel" />   
      <!-- <el-table-column label="确认收货时间" align="center" prop="gmtConfirm" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtConfirm, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="供应商id 存在该值即说明此订单为供应商开通" align="center" prop="providerId" /> -->
      <el-table-column label="经手人" align="center" prop="providerId" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" fixed="right" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="支付用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入支付用户id" />
        </el-form-item>
        <el-form-item label="市场价" prop="price">
          <el-input v-model="form.price" placeholder="请输入市场价" />
        </el-form-item>
        <el-form-item label="应付金额" prop="actualPrice">
          <el-input v-model="form.actualPrice" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="payPrice">
          <el-input v-model="form.payPrice" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paytime">
          <el-date-picker clearable
            v-model="form.paytime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付流水号" prop="payNo">
          <el-input v-model="form.payNo" placeholder="请输入支付流水号" />
        </el-form-item>
        <el-form-item label="订单备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入订单备注" />
        </el-form-item>
        <el-form-item label="支付渠道：ALI支付宝  WX微信  EX兑换码兑换" prop="payChannel">
          <el-input v-model="form.payChannel" placeholder="请输入支付渠道：ALI支付宝  WX微信  EX兑换码兑换" />
        </el-form-item>
        <el-form-item label="收货省份，若province字段为空，表示无需收货地址" prop="province">
          <el-input v-model="form.province" placeholder="请输入收货省份，若province字段为空，表示无需收货地址" />
        </el-form-item>
        <el-form-item label="收货城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入收货城市" />
        </el-form-item>
        <el-form-item label="收货县区" prop="county">
          <el-input v-model="form.county" placeholder="请输入收货县区" />
        </el-form-item>
        <el-form-item label="收货地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="开始学习天数" prop="startday">
          <el-input v-model="form.startday" placeholder="请输入开始学习天数" />
        </el-form-item>
        <el-form-item label="收货电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入收货电话" />
        </el-form-item>
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="确认收货时间" prop="gmtConfirm">
          <el-date-picker clearable
            v-model="form.gmtConfirm"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择确认收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商id 存在该值即说明此订单为供应商开通" prop="providerId">
          <el-input v-model="form.providerId" placeholder="请输入供应商id 存在该值即说明此订单为供应商开通" />
        </el-form-item>
        <el-form-item label="${comment}" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="subjectDeptId">
          <el-input v-model="form.subjectDeptId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="subjectChapterDeptId">
          <el-input v-model="form.subjectChapterDeptId" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/course/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        userId: null,
        orderStatus: null,
        price: null,
        actualPrice: null,
        payPrice: null,
        paytime: null,
        payNo: null,
        payChannel: null,
        province: null,
        city: null,
        county: null,
        address: null,
        startday: null,
        phone: null,
        consignee: null,
        gmtConfirm: null,
        providerId: null,
        subjectId: null,
        subjectDeptId: null,
        subjectChapterDeptId: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "支付用户id不能为空", trigger: "blur" }
        ],
        actualPrice: [
          { required: true, message: "应付金额不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "收货省份，若province字段为空，表示无需收货地址不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "收货城市不能为空", trigger: "blur" }
        ],
        county: [
          { required: true, message: "收货县区不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "收货地址不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "收货电话不能为空", trigger: "blur" }
        ],
        consignee: [
          { required: true, message: "收货人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        userId: null,
        orderStatus: null,
        price: null,
        actualPrice: null,
        payPrice: null,
        paytime: null,
        payNo: null,
        remark: null,
        payChannel: null,
        province: null,
        city: null,
        county: null,
        address: null,
        startday: null,
        phone: null,
        consignee: null,
        gmtConfirm: null,
        providerId: null,
        subjectId: null,
        subjectDeptId: null,
        subjectChapterDeptId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
